package OnlineShopping;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class Product {
    private String productId;
    private String productName;
    private double price;
    // 构造函数
    public Product(String productId, String productName, double price) {
        this.productId = productId;
        this.productName = productName;
        this.price = price;
    }
    //添加商品
    public void addProduct() {
        try (FileInputStream fis = new FileInputStream("E:\\Desktop\\大二上\\实训\\网上购物系统\\Good.xlsx");
             Workbook workbook = new XSSFWorkbook(fis)) {
            Sheet sheet = workbook.getSheetAt(0);
            int lastRow = sheet.getLastRowNum();
            Row newRow = sheet.createRow(lastRow + 1);

            newRow.createCell(0).setCellValue(productId); // 商品ID
            newRow.createCell(1).setCellValue(productName); // 商品名称
            newRow.createCell(5).setCellValue(price); // 商品价格

            try (FileOutputStream fos = new FileOutputStream("E:\\Desktop\\大二上\\实训\\网上购物系统\\Good.xlsx")) {
                workbook.write(fos);
            }

            System.out.println("商品添加成功: " + productName);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //修改商品信息
    public void modifyProduct(String productId) {
        try (FileInputStream fis = new FileInputStream("E:\\Desktop\\大二上\\实训\\网上购物系统\\Good.xlsx");
             Workbook workbook = new XSSFWorkbook(fis)) {
            Sheet sheet = workbook.getSheetAt(0);

            for (Row row : sheet) {
                Cell idCell = row.getCell(0);
                if (idCell != null && idCell.getStringCellValue().equals(productId)) {
                    row.getCell(1).setCellValue(productName); // 修改商品名称
                    row.getCell(5).setCellValue(price); // 修改商品价格

                    try (FileOutputStream fos = new FileOutputStream("E:\\Desktop\\大二上\\实训\\网上购物系统\\Good.xlsx")) {
                        workbook.write(fos);
                    }
                    System.out.println("商品信息修改成功: " + productName);
                    return;
                }
            }
            System.out.println("未找到商品ID: " + productId);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //删除商品
    public void deleteProduct(String productId) {
        try (FileInputStream fis = new FileInputStream("E:\\Desktop\\大二上\\实训\\网上购物系统\\Good.xlsx");
             Workbook workbook = new XSSFWorkbook(fis)) {
            Sheet sheet = workbook.getSheetAt(0);

            for (int i = 0; i <= sheet.getLastRowNum(); i++) {
                Row row = sheet.getRow(i);
                Cell idCell = row.getCell(0);
                if (idCell != null && idCell.getStringCellValue().equals(productId)) {
                    sheet.removeRow(row);
                    System.out.println("商品删除成功: " + productId);
                    break;
                }
            }

            try (FileOutputStream fos = new FileOutputStream("E:\\Desktop\\大二上\\实训\\网上购物系统\\Good.xlsx")) {
                workbook.write(fos);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //查看所有商品
    public void viewProducts() {
        try (FileInputStream fis = new FileInputStream("E:\\Desktop\\大二上\\实训\\网上购物系统\\Good.xlsx");
             Workbook workbook = new XSSFWorkbook(fis)) {
            Sheet sheet = workbook.getSheetAt(0);

            System.out.println("商品列表: ");
            for (Row row : sheet) {
                Cell idCell = row.getCell(0);
                Cell nameCell = row.getCell(1);
                Cell priceCell = row.getCell(2);
                if (idCell != null && nameCell != null && priceCell != null) {
                    System.out.println("ID: " + idCell.getStringCellValue() + ", 名称: "
                            + nameCell.getStringCellValue() + ", 价格: "
                            + priceCell.getNumericCellValue());
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
